/**
* 仓库主页面
*/
<template>
	<el-container style="padding: 10px 10px 0 10px;">
		<el-aside width="700px">
			<vp-card>
				<vp-table :data="tableData" row-key="id" highlight-current-row ref="tableRef"
					@row-click="handleClickModule" style="width: 100%;" border stripe v-loading="loading"
					:height="`calc(100vh - 164px)`">
					<el-table-column prop="bh" show-overflow-tooltip>
						<template #header><vp-label zh="仓库" en="Warehouse" /></template>
						<template #default="scope">
							<p>{{ scope.row.bh }}</p>
							<p>{{ scope.row.mc }}</p>
						</template>
					</el-table-column>
					<el-table-column prop="jdName" show-overflow-tooltip :width="150">
						<template #header><vp-label zh="基地" en="Base" /></template>
					</el-table-column>
					<el-table-column prop="lb" show-overflow-tooltip :width="150">
						<template #header><vp-label zh="类别" en="Type" /></template>
						<template #default="scope">
							<vp-label v-if="scope.row.lb === 10" zh="可用库" en="Available Warehouse" />
							<vp-label v-else-if="scope.row.lb === 20" zh="待修库" en="To Be Repaired Warehouse" />
							<vp-label v-else-if="scope.row.lb === 30" zh="报废库" en="Scrap Warehouse" />
							<vp-label v-else-if="scope.row.lb === 40" zh="隔离库" en="Isolated Warehouse" />
						</template>
					</el-table-column>
					<el-table-column width="100">
						<template #header><vp-label-operations /></template>
						<template #default="scope">
							<el-space>
								<vp-btn-edit text v-auth="'masterDataWarehouse.edit'" @click="handleEdit(scope.row)" />
								<vp-btn-del text v-auth="'masterDataWarehouse.edit'" @click="handleDelete(scope.row)" />
							</el-space>
						</template>
					</el-table-column>
				</vp-table>
				<vp-btn-add type="primary" class="mt5 w100" v-auth="'masterDataWarehouse.edit'" @click="handleAdd" />
			</vp-card>
		</el-aside>

		<el-main class="layout-parent">
			<LocationTable :module="currentModule"></LocationTable>
		</el-main>
		<!-- 新增/修改模块页面 -->
		<EditWarehouse ref="editWarehouseRef" @form-submited="loadDataList"></EditWarehouse>
	</el-container>
</template>

<script setup lang="ts" name="masterDataWarehouse">
import { ref, onMounted, defineAsyncComponent } from 'vue';
import request from '/@/utils/request';
import LocationTable from './copmonent/LocationTable.vue';
import VpMessageBox from '/@/components/vp/VpMessageBox';

const EditWarehouse = defineAsyncComponent(() => import('./copmonent/dialogs/EditWarehouse.vue'));

// 当前选中模块
const currentModule = ref({
	id: "",
	bh: "",
	mc: "",
});

const loading = ref(false);
const tableData = ref([]);
const tableRef = ref();
const editWarehouseRef = ref();

// 加载列表数据
const loadDataList = () => {
	loading.value = true;
	request.get('/masterData/warehouse/list').then(res => {
		tableData.value = res?.data || [];
		if (tableData.value && tableData.value.length > 0) {
			// 如果模块没有被选中，则默认选中第一个模块
			if (!currentModule.value.id) {
				currentModule.value = {
					...tableData.value[0] as any,
				}
			}
			// 选中指定行
			tableRef.value.setCurrentRow(currentModule.value);
		}
	}).finally(() => {
		loading.value = false
	})
}

// 组件挂载后事件
onMounted(() => {
	loadDataList()
})

// 点击模块列表
const handleClickModule = (module: any) => {
	currentModule.value = {
		...module
	}
}

// 新增模块
const handleAdd = () => {
	editWarehouseRef.value.handleOpen('add');
}

// 修改模块
const handleEdit = (row: any) => {
	editWarehouseRef.value.handleOpen('edit', row.id);
}
// 删除模块
const handleDelete = (row: any) => {
	VpMessageBox.confirmDelete().then(() => {
		request.get('/masterData/warehouse/location/list', {
			params: {
				mainid: row.id,
			}
		}).then(res => {
			let list = res?.data?.list || []
			if (list.length) {
				// 如模块下包含部件，则进行二次确认后再删除
				VpMessageBox.confirm({
					zh: '模块下包含部件信息，是否继续删除？',
					en: 'The module contains component information. Do you want to continue deleting it?',
				}, {
					zh: '提示',
					en: 'Info',
				}, {
					type: 'warning',
				}).then(() => {
					request.delete(`/masterData/warehouse/${row.id}`).then(res => {
						currentModule.value.id = '';
						loadDataList();
					})
				})
			} else {
				request.delete(`/masterData/warehouse/${row.id}`).then(res => {
					currentModule.value.id = '';
					loadDataList();
				})
			}
		})
	});
}


</script>
<style scoped lang="scss">
.layout-parent {
	margin-top: -10px;
}
</style>
